<template>
  <div v-if="data" class="tree">
    <div class="block">
      <div class="title">前置页</div>
      <div v-if="(data.frontMatter || []).length">
        <div v-for="fm in data.frontMatter" :key="idOf(fm)" :class="['item', classForFM(fm)]">
          <span>• {{ fm.customTitle || ('表单 ' + (fm.formId || '')) }}</span>
        </div>
      </div>
      <div v-else class="empty">无</div>
    </div>

    <div class="block">
      <div class="title">访视与表单</div>
      <div v-if="(data.sections || []).length">
        <div v-for="sec in data.sections" :key="idOf(sec)" :class="['section', classForSection(sec)]">
          <div class="section-title">■ {{ sec.name || sec.title || '-' }}</div>
          <div v-if="(sec.forms || []).length">
            <div v-for="f in sec.forms" :key="idOf(f)" :class="['form', classForForm(f)]">
              <div class="form-title">- {{ f.name || f.title || '-' }}
                <span v-if="nameOnlyChanged(f)" class="tag-name-change" title="仅表单名称变更，字段未变化">名称</span>
              </div>
              <div v-if="shouldRenderFieldsForForm(f)" class="fields">
                <div v-for="fld in (f.fields || [])" :key="idOf(fld)" :class="['field', classForField(fld)]">
                  <span>· {{ fld.value?.label || fld.value?.fieldName || fld.name || '-' }}</span>
                </div>
              </div>
            </div>
          </div>
          <div v-else class="empty">无表单</div>
        </div>
      </div>
      <div v-else class="empty">无访视</div>
    </div>
  </div>
  <div v-else class="empty">暂无数据</div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  snapshot: Object,
  changedIds: {
    type: Object,
    default: () => ({ sections: [], forms: [], fields: [], frontMatters: [] })
  },
  changes: {
    type: Object,
    default: null
  }
})

const data = computed(() => props.snapshot || null)
const idOf = (x) => x?._id || x?.id || x?.formId || x?.sectionId || x?.fieldId

const isIn = (arr, x) => {
  const id = idOf(x)
  return !!id && Array.isArray(arr) && arr.includes(id)
}
// 分类高亮：新增=green、删除=red、修改=yellow；没有分类时回退为统一高亮
function classForSection(sec){
  const id = idOf(sec)
  const c = props.changes?.sections
  if (c){
    if (c.added?.includes(id)) return 'added'
    if (c.removed?.includes(id)) return 'removed'
    if (c.modified?.includes(id)) return 'modified'
  }
  return isIn(props.changedIds.sections, sec) ? 'modified' : ''
}
function classForForm(f){
  const id = idOf(f)
  const c = props.changes?.forms
  if (c){
    if (c.added?.includes(id)) return 'added'
    if (c.removed?.includes(id)) return 'removed'
    if (c.modified?.includes(id)) return 'modified'
  }
  return isIn(props.changedIds.forms, f) ? 'modified' : ''
}
function classForField(fld){
  const id = idOf(fld)
  const c = props.changes?.fields
  if (c){
    if (c.added?.includes(id)) return 'added'
    if (c.removed?.includes(id)) return 'removed'
    if (c.modified?.includes(id)) return 'modified'
  }
  return isIn(props.changedIds.fields, fld) ? 'modified' : ''
}
function classForFM(fm){
  const id = idOf(fm)
  const c = props.changes?.frontMatters
  if (c){
    if (c.added?.includes(id)) return 'added'
    if (c.removed?.includes(id)) return 'removed'
    if (c.modified?.includes(id)) return 'modified'
  }
  return isIn(props.changedIds.frontMatters, fm) ? 'modified' : ''
}

// 仅名称变更时：不渲染该表单的字段（减少噪音）
const fieldChangedSet = computed(() => {
  const arr = [
    ...(props.changes?.fields?.added || []),
    ...(props.changes?.fields?.removed || []),
    ...(props.changes?.fields?.modified || [])
  ]
  return new Set(arr)
})
const formAddedSet = computed(() => new Set(props.changes?.forms?.added || []))
const formRemovedSet = computed(() => new Set(props.changes?.forms?.removed || []))
const formModifiedSet = computed(() => new Set(props.changes?.forms?.modified || []))

function shouldRenderFieldsForForm(form){
  const fid = idOf(form)
  // 新增/删除表单：完整展示
  if (formAddedSet.value.has(fid) || formRemovedSet.value.has(fid)) return true
  // 名称修改但字段完全未变：仅渲染表单标题
  if (formModifiedSet.value.has(fid)){
    const anyFieldChanged = (form.fields || []).some(ff => fieldChangedSet.value.has(idOf(ff)))
    return anyFieldChanged
  }
  // 未标记修改：保持原样（渲染字段）
  return true
}

function nameOnlyChanged(form){
  const fid = idOf(form)
  if (!formModifiedSet.value.has(fid)) return false
  const anyFieldChanged = (form.fields || []).some(ff => fieldChangedSet.value.has(idOf(ff)))
  return !anyFieldChanged
}
</script>

<style scoped>
.tree { font-size: 13px; line-height: 1.7; }
.block { margin-bottom: 10px; }
.title { font-weight: 600; margin-bottom: 6px; }
.section { margin: 6px 0; }
.section-title { font-weight: 600; }
.form { margin: 4px 0 4px 8px; }
.form-title { color: #333; }
.tag-name-change { margin-left: 6px; padding: 1px 6px; font-size: 12px; color: #8a6d3b; background: #fff7e6; border: 1px solid #f3d19e; border-radius: 3px; }
.fields { margin-left: 12px; }
.item, .field { padding: 2px 4px; border-radius: 3px; }
.empty { color: #909399; padding: 4px 0; }
.modified { background: #fff7e6; border-left: 3px solid #e6a23c; }
.added { background: #f0f9eb; border-left: 3px solid #67c23a; }
.removed { background: #fef0f0; border-left: 3px solid #f56c6c; }
</style>




